package com.hbzhit.common.ueditor.controller;

import com.hbzhit.common.ueditor.ActionEnter;
import com.hbzhit.modules.oss.config.OssProperties;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: hobbesgo
 * @Description:
 * @Date: 创建于 14:54 2018/1/11 0011
 * @Modified By:
 */
@Controller
@AllArgsConstructor
@RequestMapping("/ueditor")
public class UEditorController {

    private final OssProperties ossProperties;
    private final ServerProperties serverProperties;

    @RequestMapping(value = "/config")
    public void config(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("application/json");
        // String rootPath = request.getSession().getServletContext().getRealPath("/");
        String rootPath = "/ueditor";
        try {
            String exec = new ActionEnter(request, rootPath, ossProperties.getFileUploadConfig().getPath().replaceAll("\\\\", ""), serverProperties.getServlet().getContextPath()).exec();
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
